package com.lngsyz.ywtb.biz.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.ywtb.entity.source1.SystemCompany;
import com.lngsyz.ywtb.entity.source2.TCompany;
import com.lngsyz.ywtb.mapper.source2.TCompanyMapper;

import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;

@Service
public class TCompanyService {
    
	@Autowired
	TCompanyMapper tCompanyMapper;
	@Autowired
	SystemCompanyService systemCompanyService;
	
    public List<TCompany> findByCondition(String districtCity, String district, String name){
    	return findByCondition(districtCity, district, name, null);
    }
    
    public List<TCompany> findByCondition(String districtCity, String district, String name, String sealMedia){
    	List<TCompany> rtn = new ArrayList<TCompany>();
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("districtCity", districtCity);
    	if(StringUtils.isNotBlank(district)) {
    		param.put("district", district);
    	}
    	if(StringUtils.isNotBlank(name)) {
    		param.put("name", name);
    	}
    	if(StringUtils.isNotBlank(sealMedia)) {
    		param.put("sealMedia", sealMedia);
    	}
    	List<TCompany> lists = tCompanyMapper.findByCondition(param);
    	if(StringUtils.isNotBlank(district)) {
    		List<SystemCompany> systemCompanys = systemCompanyService.findByCondition(districtCity, district, name, 1, 200);
    		Map<String, SystemCompany> companyMap = systemCompanys.stream().collect(Collectors.toMap(SystemCompany::getId, (p) -> p)); 
    		for(TCompany tc : lists) {
    			if(companyMap.containsKey(tc.getId())) {
    				SystemCompany sc = companyMap.get(tc.getId());
    				if(StringUtils.equals(sc.getDistrict(), district)) {
    					rtn.add(tc);
    				}
    			}
    		}
    	} else {
    		rtn = lists;
    	}
    	return rtn;
    }
}
